﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class BackGround : MonoBehaviour {
    public Rigidbody2D rigidbody;

    float width;
    private float scrollSpeed = -5.0f;
    void Start () {

        this.rigidbody = GetComponent<Rigidbody2D> ();
        rigidbody.velocity = new Vector2 (scrollSpeed, 0);
        width = GetComponent<BoxCollider2D> ().size.x;
    }

    // Update is called once per frame
    void Update () {
        if (GameManaer.instance.isGameOver) {

            rigidbody.velocity = Vector2.zero;
            return;
        }

        // 使循环滚动
        // 若是自己的坐标已经比自己的低于自己的宽度
        if (this.transform.position.x < -width) {
            // 将自己重置到宽度的前方距离2倍长度的地方（中间间隔另一张背景）
            this.transform.position += new Vector3 (width * 2, 0, 0);

        }
    }

}